View Javadoc
1 package com.inigoserrano.isvalidator.errorDo; 2 3 import java.util.Enumeration; 4 import java.util.Hashtable; 5 6 import org.apache.regexp.RE; 7 import org.apache.regexp.RESyntaxException; 8 9 /*** 10 * This is the base class to all the ErrorDo. Usually the rest of 11 * the implementations of the ErrorDo overrides the required methods 12 * 13 * @license@ 14 * 15 * @version @version@ 16 * @author @author@ 17 **/ 18 public class SimpleErrorDo implements ErrorDo { 19 /*** 20 * To store all the parameter, with its name and value 21 */ 22 protected Hashtable parameters = null; 23 /*** 24 * To store all the error messages 25 */ 26 protected Hashtable errorMessages = null; 27 28 /*** 29 * Constructs the object with and error message 30 * @param errorMessages a Hashtable with the error messages 31 * @throws ErrorDoInternalException for internal error only 32 **/ 33 public SimpleErrorDo(Hashtable errorMessages) 34 throws ErrorDoInternalException { 35 super(); 36 if (errorMessages == null) { 37 throw new ErrorDoInternalException( 38 "Parameter \"ErrorMessages\" in " + "SimpleErrorDo is null"); 39 } 40 this.errorMessages = errorMessages; 41 } 42 43 /*** 44 * Add one parameter 45 * @param parameterName the name of the parrameter 46 * @param parameterValue the value of the parameter 47 **/ 48 public void addParameter(String parameterName, String parameterValue) { 49 if (this.parameters == null) { 50 this.parameters = new Hashtable(); 51 } 52 parameters.put(parameterName, parameterValue); 53 } 54 55 /*** 56 * Return a message processed. By default the returned message 57 * is the message string with a substitution of the key ($key;) 58 * with the value of the parameter (The key is the name of the parameter) 59 * @throws ErrorDoInternalException For internal error only 60 * @return the message 61 **/ 62 public String getMessage() throws ErrorDoInternalException { 63 //Error check 64 String message = null; 65 Enumeration iterator = null; 66 String key = null; 67 RE expresion = null; 68 if (this.parameters == null) { 69 throw new ErrorDoInternalException( 70 "Parameters is Null, the " 71 + "Constraint must add the parameter to this ErrorDo"); 72 } 73 if (parameters.get("constraint") == null) { 74 throw new ErrorDoInternalException( 75 "The parameter constraints is Null, " 76 + "the Constraint must add the parameter to this ErrorDo"); 77 } 78 message = (String) errorMessages.get(parameters.get("constraint")); 79 if (message == null) { 80 message = 81 "Not present the description for the Constraint $constraint;." 82 + " The value \"$valueToCheck;\" is not valid."; 83 } 84 //End Error Check 85 expresion = null; 86 iterator = parameters.keys(); 87 key = null; 88 while (iterator.hasMoreElements()) { 89 key = (String) iterator.nextElement(); 90 try { 91 expresion = new RE("[$]" + key + "[;]"); 92 message = 93 expresion.subst(message, (String) parameters.get(key)); 94 } catch (RESyntaxException e) { 95 //do nothing go to the next key 96 } 97 } 98 return message; 99 } 100 101 /*** 102 * Return a new Instace of this ErrorDo 103 * @return a new ErrorDo 104 **/ 105 public ErrorDo getNewInstance() { 106 Enumeration iterator = null; 107 String key = null; 108 try { 109 SimpleErrorDo newInstance = new SimpleErrorDo(this.errorMessages); 110 if (this.parameters == null) { 111 return newInstance; 112 } else { 113 iterator = parameters.keys(); 114 while (iterator.hasMoreElements()) { 115 key = (String) iterator.nextElement(); 116 newInstance.addParameter(key, (String) parameters.get(key)); 117 } 118 return newInstance; 119 } 120 } catch (ErrorDoInternalException e) { 121 //do nothing is imposible 122 return null; 123 } 124 } 125 }

This page was automatically generated by Maven